<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
	String xm_id = request.getParameter("xm_id") == null ? "" : request.getParameter("xm_id");
	String xm_code = request.getParameter("xm_code") == null ? "" : request.getParameter("xm_code");
	String xm_name = request.getParameter("xm_name") == null ? "" : request.getParameter("xm_name");
	int isFlowShow = request.getParameter("isFlowShow") == null ? 0 : Integer.parseInt(request.getParameter("isFlowShow"));
%>
<html>
	<head>
		<title>打印预览</title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
			.datagrid-cell {
			  font-size: 18px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
		var submitUrl = '<%=contextPath %>/springMVC/xm/addXMGDBalance';
		$(function(){
			var url = '<%=contextPath %>/springMVC/xm/listXMGDBalance?type=0&xm_id=<%=xm_id%>';
			$.ajax({
				type:'POST',
				url:'<%=contextPath %>/springMVC/xm/hasXMBalance?type=0&xm_id=<%=xm_id%>',
				async:false,
				success:function(rtjson){
					if(rtjson != '0'){
						url = '<%=contextPath %>/springMVC/xm/listXMGDBalanceByXmId?type=0&xm_id=<%=xm_id%>';
						submitUrl = '<%=contextPath %>/springMVC/xm/updateXMGDBalance';
						$('#submitValue').html("重新保存");
					}
				}
			});
			$('#table').datagrid({
				url:url,
				rownumbers:true,
				singleSelect:true,
				showFooter:true,
				fitColumns:true,
				frozenColumns:[[
				    {field:'id',hidden:true},
				    {title:'产品id',field:'material_id',hidden:true}
				]],
				columns:[[
					{title:'产品名称',field:'name',width:200,align:'center',rowspan:2},
					{title:'规格/型号',field:'format_type',width:200,align:'center',rowspan:2},
					{title:'单位',field:'unit_name',width:80,align:'center',rowspan:2},
					{title:'甲供领用数',field:'customer_num',width:200,align:'center',rowspan:2},
					{title:'订单',width:200,align:'center',colspan:2},
					{title:'现场新用数',field:'xc_use',width:150,align:'center',rowspan:2},
					{title:'现场利旧',width:200,align:'center',colspan:2},
					{title:'施工队',width:200,align:'center',colspan:2},
					{title:'订单工余料',field:'order_left',width:150,align:'center',rowspan:2,formatter:function(value,rowData,index){
						rowData.order_left = parseFloat(rowData.purchase_num)-parseFloat(rowData.purchase_use_num);
						if(isNaN(rowData.order_left)){
							return "";
						}
						return rowData.order_left;
					}},
					{title:'拆旧回收',width:200,align:'center',colspan:2},
					{title:'订单领用总额',field:'purchase_use_cost',hidden:true},
					{title:'材料经济分析',width:600,align:'center',colspan:2},
					{title:'项目统计项',width:600,align:'center',colspan:5},
				],[
				
					{title:'采购数',field:'purchase_num',width:100,align:'center'},
					{title:'领用数',field:'purchase_use_num',width:100,align:'center'},
					{title:'利旧数',field:'use_old_num',width:100,align:'center'},
					{title:'总价',field:'use_old_cost',width:80,align:'center'},
					{title:'工余料',field:'worker_left_num',width:100,align:'center',formatter:function(value,rowData,index){
						rowData.worker_left_num = parseFloat(rowData.xc_use)-parseFloat(rowData.purchase_use_num)-parseFloat(rowData.customer_num);
						if(isNaN(rowData.worker_left_num)){
							return "";
						}
						return rowData.worker_left_num;
					}},
					{title:'单价',field:'worker_left_cost',width:80,align:'center'},
					{title:'回收数',field:'recovery_num',width:100,align:'center'},
					{title:'总价',field:'recovery_cost',width:80,align:'center'},
					{title:'材料采购费',field:'purchase_cost',width:150,align:'center'},
					{title:'施工队材料款',field:'c',width:200,align:'center',formatter:function(value,rowData,index){
						if(rowData.name == '合计:'){
							return value;
						}
						rowData.worker_money = Math.round(parseFloat(rowData.worker_left_num)*parseFloat(rowData.worker_left_cost)*Math.pow(10,2))/Math.pow(10,2);
						return rowData.worker_money;
					}},
					
					{title:'决算实用数',field:'js_real_num',width:100,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2
						}
					}},
					{title:'决算单价',field:'js_per_price',width:80,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2
						}
					}},
					{title:'公司工余料',field:'js_company_left',width:150,align:'center',formatter:function(value,rowData,index){
						if(rowData.name == '合计:'){
							if(isNaN(rowData.js_company_left)){
								return 0;
							}
							return value;
						}
						rowData.js_company_left = parseFloat(rowData.js_real_num)-parseFloat(rowData.customer_num);
						if(isNaN(rowData.js_company_left)){
							return 0;
						}
						return rowData.js_company_left;
					}},
					{title:'材料挣值',field:'js_material_profit',width:150,align:'center',formatter:function(value,rowData,index){
			
						if(rowData.name == '合计:'){
							if(isNaN(rowData.js_material_profit)){
							return 0;
							}
							return value;
						}

						rowData.js_material_profit =parseFloat(rowData.js_company_left)*parseFloat(rowData.js_per_price)+parseFloat(rowData.recovery_cost)-parseFloat(rowData.worker_money);
						if(isNaN(rowData.js_material_profit)){
							return 0;
						}
						return rowData.js_material_profit;
					}},
					{title:'材料管控指标',field:'js_material_manage',width:200,align:'center',formatter:function(value,rowData,index){
			
						if(rowData.name == '合计:'){
							if(isNaN(rowData.js_material_manage)){
							return 0;
							}
							return value;
						}

						rowData.js_material_manage =parseFloat(rowData.js_material_profit)-parseFloat(rowData.purchase_cost);
						if(isNaN(rowData.js_material_manage)){
							return 0;
						}
						return rowData.js_material_manage;
					}}
				   ]],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onAfterEdit:function(){
					showFooter();
				},
				onLoadSuccess:function(data){
					showFooter();
				}
			});
		});
		function showFooter(){
			var jsondata=$("#table").datagrid("getData");
			var purchase_cost_total = 0;
			var worker_money_total = 0;
			var js_real_num_total=0;
			var js_company_left_total=0;
			var js_material_profit_total=0;
			var js_material_manage_total=0;
			for(var i=0;i<jsondata.total;i++){
				purchase_cost_total += jsondata.rows[i].purchase_cost;
				worker_money_total += jsondata.rows[i].worker_money;
				js_real_num_total+=parseFloat(jsondata.rows[i].js_real_num);
				js_company_left_total+=parseFloat(jsondata.rows[i].js_company_left);
				js_material_profit_total+=parseFloat(jsondata.rows[i].js_material_profit);
				js_material_manage_total+=jsondata.rows[i].js_material_manage;
			}
			$('#table').datagrid('reloadFooter',[
			                              	{name:'合计:',
			                              	 purchase_cost: purchase_cost_total, 
			                              	 worker_money: worker_money_total,
			                              	 //js_real_num:js_real_num_total,
			                              	 js_company_left:js_company_left_total,
			                              	 js_material_profit:js_material_profit_total,
			                              	 js_material_manage:js_material_manage_total
			                              	 }
			                              ]);
		}
		
		function startPrint(obj){
			$(obj).hide();
			window.print();
		}
	</script>
	</head>
	<body>
		<div >
		<form id="ff">
			<div style="text-align: center;">
					<p style="font-weight: bolder;">甲供材料平衡</p>
			</div>
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>项目编号：</td>
					<td class="td2" nowrap>
						<span  id="xm_code" name="xm_code"  ><%=xm_code%></span>
					</td>
					<td align="right" class="td1" nowrap>项目名称：</td>
					<td class="td2" nowrap>
						<span  id="xm_name" name="xm_name" ><%=xm_name%></span>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>
					</td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-print',plain:false" onclick="startPrint(this);">确认打印</a>
						</div>
					</td>
				</tr>
			</table>
			</form>
			<div id="table" ></div>
		</div>
	</body>
</html>